package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.AnimalMarkDao;
import com.jiantong.work.model.AnimalMark;
import com.jiantong.work.service.AnimalMarkWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 6/7/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class AnimalMarkWriteServiceImpl implements AnimalMarkWriteService{

    private final AnimalMarkDao animalMarkDao;

    @Autowired
    public AnimalMarkWriteServiceImpl(AnimalMarkDao animalMarkDao){
        this.animalMarkDao = animalMarkDao;
    }

    @Override
    public Response<Long> createAnimalMark(AnimalMark animalMark) {

        try{
            animalMarkDao.create(animalMark);
            return Response.ok(animalMark.getId());
        }catch (Exception e){
            log.error("failed to create animal mark", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to create animal mark");
        }
    }

    @Override
    public Response<Integer> createAnimalMarks(List<AnimalMark> animalMarks) {
        try{
            return Response.ok(animalMarkDao.creates(animalMarks));
        }catch (Exception e){
            log.error("failed to create animal marks", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to create animal marks");
        }
    }

    @Override
    public Response<Boolean> updateAnimalMark(AnimalMark animalMark) {
        try{
            return Response.ok(animalMarkDao.update(animalMark));
        }catch (Exception e){
            log.error("failed to update animal mark", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to update animal mark");
        }
    }

    /**
     * 根据耳标号markNumber更新
     *
     * @param animalMark
     * @return
     */
    @Override
    public Response<Boolean> updateAnimalMarkByNumber(AnimalMark animalMark) {
        try{
            return Response.ok(animalMarkDao.updateByNumber(animalMark));
        }catch (Exception e){
            log.error("failed to update animal mark by markNumber", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to update animal mark  by markNumber");
        }
    }

    /**
     * 更新一个List<AnimalMark>
     *
     * @param map
     * @return
     */
    @Override
    public Response<Boolean> updates(Map<String, Object> map) {
        try{
            return Response.ok(animalMarkDao.updates(map));
        }catch (Exception e){
            log.error("failed to update animal marks", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to update animal marks");
        }
    }
}

